/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import gurobi.GRBException;
import model.EnhancedCostEvaluator;
import model.Problem;
import model.Solution;
import solver.MathSolverLC_CV_Final;
import util.Chronograph;
import util.Importer;
import util.SolutionImporter;

/**
 * 
 * @author Administrator
 */
public class MainCV {

	static Problem problem;
	static String[] initial_solutions;

	static {
		// System.load("/home/chgogos/or-tools-read-only/libjnilinearsolver.so");
		System.loadLibrary("jnilinearsolver");
	}

	public static void main(String[] args) throws GRBException {
		Chronograph chronos = new Chronograph();
		chronos.reset();
		Importer importer;
		SolutionImporter solutionImporter;

		String[] datasets;

		datasets = new String[11];
		initial_solutions = new String[11];
		datasets[0] = "datasets//model_example.txt";
		datasets[1] = "datasets//model_a1_1.txt";
		datasets[2] = "datasets//model_a1_2.txt";
		datasets[3] = "datasets//model_a1_3.txt";
		datasets[4] = "datasets//model_a1_4.txt";
		datasets[5] = "datasets//model_a1_5.txt";
		datasets[6] = "datasets//model_a2_1.txt";
		datasets[7] = "datasets//model_a2_2.txt";
		datasets[8] = "datasets//model_a2_3.txt";
		datasets[9] = "datasets//model_a2_4.txt";
		datasets[10] = "datasets//model_a2_5.txt";
		initial_solutions[0] = "solutions//assignment_example.txt";
		initial_solutions[1] = "solutions//assignment_a1_1.txt";
		initial_solutions[2] = "solutions//assignment_a1_2.txt";
		initial_solutions[3] = "solutions//assignment_a1_3.txt";
		initial_solutions[4] = "solutions//assignment_a1_4.txt";
		initial_solutions[5] = "solutions//assignment_a1_5.txt";
		initial_solutions[6] = "solutions//assignment_a2_1.txt";
		initial_solutions[7] = "solutions//assignment_a2_2.txt";
		initial_solutions[8] = "solutions//assignment_a2_3.txt";
		initial_solutions[9] = "solutions//assignment_a2_4.txt";
		initial_solutions[10] = "solutions//assignment_a2_5.txt";

		int problemno = 10;

		importer = new Importer(datasets[problemno]);
		importer.readData();
		problem = importer.getProblem();
		Problem.VERBOSE = false;
		System.out.println(problem.getCondensedInfo());
		solutionImporter = new SolutionImporter(problem);
		solutionImporter.setSolutionFileName(initial_solutions[problemno]);
		Solution initial_solution = solutionImporter.readData();

		Problem.RANDOM_SEED = 821L;
		boolean useGRB = false;

		MathSolverLC_CV_Final solver = new MathSolverLC_CV_Final(problem,
				initial_solution, useGRB);
		solver.solveWithTimeLimit(120);

		Solution final_solution = solver.getCurrentSolution();
		saveSolution(problemno, initial_solution, final_solution, "CV");
		chronos.lap();
		System.out.println("Time elapsed in seconds "
				+ chronos.getElapsedTimeInSeconds());
	}

	static private void saveSolution(int problemno, Solution initial_solution,
			Solution new_solution, String time_limit) {
		EnhancedCostEvaluator ece = new EnhancedCostEvaluator();
		ece.setSolutionPair(initial_solution, new_solution);
		ece.computeCost();
		// ece.printCostAnalysis();
		boolean f = ece.isFeasible();
		String sol_filename = initial_solutions[problemno].replaceAll("\\.txt",
				"");
		String fn = String.format("%s_%s%s[%s].txt", sol_filename,
				ece.getNewSolutionCost(), f ? "" : "_inf", time_limit);
		new_solution.exportToFileName(fn);
		System.out.println("File " + fn + " saved!");
	}
}
